package cn.edu.hubu.hubusmartmedicalsystem.controller;

import cn.edu.hubu.hubusmartmedicalsystem.entity.Patient;
import cn.edu.hubu.hubusmartmedicalsystem.service.PatientService;
import cn.edu.hubu.hubusmartmedicalsystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class PatientController {
    @Autowired
    private PatientService patientService;

    @RequestMapping("/patient/check")
    public ResultVO check(String account){
        //System.out.println("---------check");
        ResultVO vo=patientService.checkAccount(account);
        return vo;

    }
    @RequestMapping("/patient/regist")
    public ResultVO regist(Patient patient){
        //System.out.println("---------regist");
        ResultVO vo=patientService.savePatient(patient);
        return vo;
    }
    //登录
    @RequestMapping("/patient/login")
    public ResultVO login(String patientId, String patientPassword) {
        ResultVO result = patientService.loginPatient(patientId, patientPassword);
        if (result == null) {
            // 返回错误响应，防止 null 值
            return new ResultVO(401, "登录失败，用户名或密码错误", null);
        }
        return result;
    }
    // 根据患者ID查询患者信息
    @RequestMapping("/patient/getPatientById")
    public ResultVO getPatientById(String patientId) {
        System.out.println("---------getPatientById");
       ResultVO<Patient> result = patientService.getPatientById(patientId);
       return result;

    }

    // 查询所有患者信息
    @RequestMapping("/patient/getAllPatients")
    public ResultVO getAllPatients() {
        ResultVO<List<Patient>> result = patientService.getAllPatients();
        return result;
    }

    // 删除患者信息
    @RequestMapping("/patient/deleteById")
    public ResultVO deleteById(String patientId) {
        ResultVO<String> result = patientService.deleteById(patientId);
        return result;
    }

    // 更新患者信息
    @RequestMapping("/patient/update")
    public ResultVO updatePatient(Patient patient) {
        ResultVO<String> result = patientService.updatePatient(patient);
        return result;
    }
}
